<template>
  <div class="app-container">
    模糊查询条件
    <!--查询表单-->
    <el-card class="operate-container" shadow="never">
      <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="searchObj.name" placeholder="考生姓名" /> 
      </el-form-item>

      <el-form-item label="邮箱">
        <el-input v-model="searchObj.email" placeholder="考生邮箱" /> 
      </el-form-item>

      <el-form-item label="分数区间">
        <el-input v-model="searchObj.lowest" placeholder="最低分" /> 
      </el-form-item>

      <el-form-item label="—">
        <el-input v-model="searchObj.maximum" placeholder="最高分" /> 
      </el-form-item>

        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    </el-card>

    <!-- 工具按钮 -->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button class="btn-add" @click="batchRemove()" >批量删除</el-button>
    </el-card>
    <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      @selection-change="handleSelectionChange">
      <el-table-column type="selection"/>
      <el-table-column prop="id" label="id" width="50" />
      <el-table-column prop="name" label="姓名" width="100" />
      <el-table-column label="邮箱" prop="email" width="250"></el-table-column>
      <el-table-column prop="result" label="加权平均分" width="200" />
      <el-table-column prop="total" label="总成绩" width="200" />
      <el-table-column label="操作" width="270" align="center">
        <template slot-scope="scope">
          <el-button type="text"  @click="removeById(scope.row.id)">删除</el-button>

          <el-button type="text" @click="getidbyinfo(scope.row.id)">修改</el-button>
          <el-dialog title="考生信息修改" :visible.sync="dialogFormVisible">
            <el-form :model="form">
              <el-form-item label="考生姓名" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="考生邮箱" :label-width="formLabelWidth">
                <el-input v-model="form.email" :disabled="true" autocomplete="off"></el-input>
              </el-form-item>
              
              <el-form-item label="加权平均分" :label-width="formLabelWidth">
                <el-input v-model="form.result" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="总成绩" :label-width="formLabelWidth">
                <el-input v-model="form.total" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button  @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="updatecandidate()">确 定</el-button>
            </div>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>

      <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>
<script>
import indexApi from '@/api/examination/index.js'
import indexApi1 from '@/api/certification/index.js'
export default {
  // 定义数据模型
   data() {
    return {
      list: [], // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 5, // 每页记录数
      searchObj: {}, // 查询条件
      multipleSelection: [],// 批量删除选中的记录列表
      dialogFormVisible: false,
          form: {
              id:'',
              name: '',
              email: '',
              result:'',
              date1: '',
              date2: '',
              delivery: false,
              type: [],
              resource: '',
              desc: '',
              total:''
        },
        formLabelWidth: '120px'

    }
  },
  created() { // 在渲染之前执行
        
  
    },
    mounted() { // 在渲染之后执行   
        this.fetchData()
       
    },
  
  // 定义方法
    methods: {


    fetchData() {
      // 调用api
      indexApi.pageList(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.records
        this.total = response.data.total
      })
    },

//根据id获取考生信息
    getidbyinfo(id){
        indexApi.getidbyinfo(id).then(response => {
            this.dialogFormVisible = true
            this.form=response.data
        })
    },

    updatecandidate(){
      this.$confirm('此操作将永久修改该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
           const examineinfo = {
            id: this.form.id,
            name:this.form.name,
            email: this.form.email,
            total:this.form.total,
            result: this.form.result
          }
          indexApi.updatecandidate(examineinfo).then(response => {
              this.dialogFormVisible = false
              this.fetchData()
      })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消修改'
          });          
        });
      
    },
     // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },

    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },

    //根据id删除
    removeById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return indexApi.removeById(id)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.message)
      })
    },
    
    handleSelectionChange(selection){
      this.multipleSelection=selection
    },



    batchRemove(){
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择要删除的记录！')
        return
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击确定，远程调用ajax
        // 遍历selection，将id取出放入id列表
        var idList = []
        this.multipleSelection.forEach(item => {
          idList.push(item.id)
          console.log(idList)
        })
        // 调用api
        return indexApi.Batchdeleter(idList)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.message)
      }).catch(error => {
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
    
  }
}
</script>